#include "kernel_operator.h"

using namespace AscendC;

constexpr int32_t BUFFER_NUM = 2;

class KernelDivFP32
{
    using T = float;

public:
    __aicore__ inline KernelDivFP32() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t coreDataNum,
                                uint32_t finalTileNum,
                                uint32_t tileDataNum,
                                uint32_t tailDataNum,
                                TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ T *)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ T *)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);

        pipe = pipeIn;
        pipe->InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(T));
        // pipe->InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        // LocalTensor<T> x1Local = inQueueX1.AllocTensor<T>();
        // LocalTensor<T> x2Local = inQueueX2.AllocTensor<T>();
        // DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        // DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        // inQueueX1.EnQue(x1Local);
        // inQueueX2.EnQue(x2Local);
        LocalTensor<T> x1Local = inQueueX1.AllocTensor<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(yLocal, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        outQueueY.EnQue(yLocal);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        // LocalTensor<T> x1Local = inQueueX1.DeQue<T>();
        // LocalTensor<T> x2Local = inQueueX2.DeQue<T>();
        // LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();

        // Div(yLocal, x1Local, x2Local, this->processDataNum);

        // outQueueY.EnQue(yLocal);
        // inQueueX1.FreeTensor(x1Local);
        // inQueueX2.FreeTensor(x2Local);
        LocalTensor<T> x1Local = inQueueX1.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();

        Div(yLocal, x1Local, yLocal, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX1.FreeTensor(x1Local);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, 1> outQueueY;

    GlobalTensor<T> x1Gm;
    GlobalTensor<T> x2Gm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

class KernelDivFP16
{
    using T = half;

public:
    __aicore__ inline KernelDivFP16() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t coreDataNum,
                                uint32_t finalTileNum,
                                uint32_t tileDataNum,
                                uint32_t tailDataNum,
                                TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ T *)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ T *)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);

        pipe = pipeIn;
        pipe->InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.AllocTensor<T>();
        LocalTensor<T> x2Local = inQueueX2.AllocTensor<T>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.DeQue<T>();
        LocalTensor<T> x2Local = inQueueX2.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();

        Div(yLocal, x1Local, x2Local, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, 1> outQueueY;

    GlobalTensor<T> x1Gm;
    GlobalTensor<T> x2Gm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

class KernelDivInt32
{
    using T = int32_t;
    using Tfp = float;

public:
    __aicore__ inline KernelDivInt32() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t coreDataNum,
                                uint32_t finalTileNum,
                                uint32_t tileDataNum,
                                uint32_t tailDataNum,
                                TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ T *)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ T *)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);

        pipe = pipeIn;
        pipe->InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));

        pipe->InitBuffer(tmpBuffer, 2 * this->tileDataNum * sizeof(Tfp));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.AllocTensor<T>();
        LocalTensor<T> x2Local = inQueueX2.AllocTensor<T>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.DeQue<T>();
        LocalTensor<T> x2Local = inQueueX2.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();
        auto x1Buf = tmpBuffer.Get<Tfp>(this->processDataNum);
        auto x2Buf = tmpBuffer.GetWithOffset<Tfp>(this->processDataNum, this->processDataNum * sizeof(Tfp));

        Cast(x1Buf, x1Local, RoundMode::CAST_NONE, this->processDataNum);
        Cast(x2Buf, x2Local, RoundMode::CAST_NONE, this->processDataNum);
        Div(x1Buf, x1Buf, x2Buf, this->processDataNum);
        Cast(yLocal, x1Buf, RoundMode::CAST_TRUNC, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, 1> outQueueY;
    TBuf<QuePosition::VECCALC> tmpBuffer;

    GlobalTensor<T> x1Gm;
    GlobalTensor<T> x2Gm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

class KernelDivInt8
{
    using T = int8_t;
    using Tfp = half;

public:
    __aicore__ inline KernelDivInt8() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                uint32_t coreDataNum,
                                uint32_t finalTileNum,
                                uint32_t tileDataNum,
                                uint32_t tailDataNum,
                                TPipe *pipeIn)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->coreDataNum = coreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = tailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ T *)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ T *)x2, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ T *)y, this->coreDataNum);

        pipe = pipeIn;
        pipe->InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(T));
        pipe->InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(T));

        pipe->InitBuffer(tmpBuffer, 2 * this->tileDataNum * sizeof(Tfp));
    }
    __aicore__ inline void Process()
    {
        uint32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (uint32_t i = 0; i < loopCount; i++)
        {
            if (i == this->tileNum - 1)
            {
                this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.AllocTensor<T>();
        LocalTensor<T> x2Local = inQueueX2.AllocTensor<T>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(uint32_t progress)
    {
        LocalTensor<T> x1Local = inQueueX1.DeQue<T>();
        LocalTensor<T> x2Local = inQueueX2.DeQue<T>();
        LocalTensor<T> yLocal = outQueueY.AllocTensor<T>();
        auto x1Buf = tmpBuffer.Get<Tfp>(this->processDataNum);
        auto x2Buf = tmpBuffer.GetWithOffset<Tfp>(this->processDataNum, this->processDataNum * sizeof(Tfp));

        Cast(x1Buf, x1Local, RoundMode::CAST_NONE, this->processDataNum);
        Cast(x2Buf, x2Local, RoundMode::CAST_NONE, this->processDataNum);
        Div(x1Buf, x1Buf, x2Buf, this->processDataNum);
        Cast(yLocal, x1Buf, RoundMode::CAST_TRUNC, this->processDataNum);

        outQueueY.EnQue(yLocal);
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
    }
    __aicore__ inline void CopyOut(uint32_t progress)
    {
        LocalTensor<T> yLocal = outQueueY.DeQue<T>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe *pipe;
    TQue<QuePosition::VECIN, 1> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, 1> outQueueY;
    TBuf<QuePosition::VECCALC> tmpBuffer;

    GlobalTensor<T> x1Gm;
    GlobalTensor<T> x2Gm;
    GlobalTensor<T> yGm;

    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

template <typename T>
class KernelDivBroadCast
{
    using Tfp = float;

public:
    __aicore__ inline KernelDivBroadCast() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y,
                                int32_t y_dimensional,
                                int32_t *y_ndarray, int32_t *x1_ndarray, int32_t *x2_ndarray,
                                int32_t *y_sumndarray, int32_t *x1_sumndarray, int32_t *x2_sumndarray)
    {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");

        this->y_dimensional = y_dimensional;

        this->y_ndarray = y_ndarray;
        this->x1_ndarray = x1_ndarray;
        this->x2_ndarray = x2_ndarray;
        this->y_sumndarray = y_sumndarray;
        this->x1_sumndarray = x1_sumndarray;
        this->x2_sumndarray = x2_sumndarray;

        x1Gm.SetGlobalBuffer((__gm__ T *)x1, 1);
        x2Gm.SetGlobalBuffer((__gm__ T *)x2, 1);
        yGm.SetGlobalBuffer((__gm__ T *)y, 1);
    }
    __aicore__ inline void Process()
    {
        Tfp x1, x2;
        int dim = this->y_dimensional;
        for (int j = 0; j < this->y_sumndarray[dim]; j++)
        {
            int x1_start = 0, x2_start = 0;
            for (int k = 0; k < dim; k++)
            {
                if (this->x1_ndarray[k] != 1)
                {
                    x1_start += this->x1_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                }
                if (this->x2_ndarray[k] != 1)
                {
                    x2_start += this->x2_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                }
            }
            x1 = static_cast<Tfp>(x1Gm.GetValue(x1_start));
            x2 = static_cast<Tfp>(x2Gm.GetValue(x2_start));
            yGm.SetValue(j, static_cast<T>(x1 / x2));
        }
    }

private:
    GlobalTensor<T> x1Gm;
    GlobalTensor<T> x2Gm;
    GlobalTensor<T> yGm;

    int32_t y_dimensional;
    int32_t *y_ndarray;
    int32_t *x1_ndarray;
    int32_t *x2_ndarray;

    int32_t *y_sumndarray;
    int32_t *x1_sumndarray;
    int32_t *x2_sumndarray;
};

extern "C" __global__ __aicore__ void div(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling)
{
    TPipe pipe;
    GET_TILING_DATA(tiling_data, tiling);
    if (TILING_KEY_IS(1))
    {
        KernelDivFP32 op;
        op.Init(x1, x2, y,
                tiling_data.coreDataNum,
                tiling_data.finalTileNum,
                tiling_data.tileDataNum,
                tiling_data.tailDataNum,
                &pipe);
        op.Process();
    }
    else if (TILING_KEY_IS(2))
    {
        KernelDivFP16 op;
        op.Init(x1, x2, y,
                tiling_data.coreDataNum,
                tiling_data.finalTileNum,
                tiling_data.tileDataNum,
                tiling_data.tailDataNum,
                &pipe);
        op.Process();
    }
    else if (TILING_KEY_IS(3))
    {
        KernelDivInt32 op;
        op.Init(x1, x2, y,
                tiling_data.coreDataNum,
                tiling_data.finalTileNum,
                tiling_data.tileDataNum,
                tiling_data.tailDataNum,
                &pipe);
        op.Process();
    }
    else if (TILING_KEY_IS(4))
    {
        KernelDivInt8 op;
        op.Init(x1, x2, y,
                tiling_data.coreDataNum,
                tiling_data.finalTileNum,
                tiling_data.tileDataNum,
                tiling_data.tailDataNum,
                &pipe);
        op.Process();
    }
    else if (TILING_KEY_IS(5))
    {
        KernelDivBroadCast<DTYPE_X1> op;
        op.Init(x1, x2, y,
                tiling_data.y_dimensional,
                tiling_data.y_ndarray, tiling_data.x1_ndarray, tiling_data.x2_ndarray,
                tiling_data.y_sumndarray, tiling_data.x1_sumndarray, tiling_data.x2_sumndarray);
        op.Process();
    }
}